<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item :label="$t('staff.search')" prop="postName">
        <el-input
          v-model="queryParams.postName"
          :placeholder="$t('operating.pleaseWrite')"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{$t('operating.search')}}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >{{$t('operating.reset')}}</el-button
        >
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:worktype:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >{{ $t('common.caddkey') }}</el-button>
      </el-col>

    <!-- <right-toolbar
        :show-search.sync="showSearch"
        @queryTable="getList"
      /> -->
    </el-row>

    <el-table ref="multipleTable" :data="postList">
      <!--&lt;!&ndash;多选框&ndash;&gt;-->
      <!--<el-table-column type="selection" width="50" align="center"></el-table-column>-->
      <!--工种名称-->
      <el-table-column
        prop="name"
        :label="$t('staff.staffTypeName')"
        min-width="200"
      />
      <!--颜色-->
      <el-table-column :label="$t('staff.color')" min-width="150">
        <template slot-scope="scope">
          <!--<el-color-picker v-model="scope.row.color"></el-color-picker>-->
          <span
            class="typeColor"
            :style="{ background: scope.row.color }"
          />
        </template>
      </el-table-column>
      <!--图标-->
      <el-table-column :label="$t('staff.icon')" min-width="200">
        <template slot-scope="scope">
          <i :class="scope.row.icon" />
        </template>
      </el-table-column>
      <!--工种说明-->
      <el-table-column
        prop="note"
        :label="$t('staff.workTypeNote')"
        min-width="150"
      />
      <!--备注-->
      <el-table-column
        prop="remark"
        :label="$t('staff.remark')"
        min-width="150"
      />
      <!--操作-->
      <el-table-column :label="$t('operating.operating')" min-width="90">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="primary" @click="seedetails()">{{
            $t("operating.seeDetails")
          }}</el-button> -->
          <el-button
            v-hasPermi="['system:worktype:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >{{ $t("operating.handle") }}</el-button>
          <el-button
            v-hasPermi="['system:worktype:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >{{ $t("operating.delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!--工种名称-->
        <el-form-item
          :label="$t('staff.staffTypeName')"
          prop="name"
          :rules="filter_rules({ required: '请输入工种名称' })"
        >
          <el-input v-model="form.name" style="max-width: 215px" />
        </el-form-item>
        <!--颜色-->
        <el-form-item :label="$t('staff.color')">
          <!-- <div class='divcolor'>
             <el-color-picker v-model="color"></el-color-picker>
          <div v-for="(item, index) in colors" :index="index" >
            <span :style="{ background: item.color }" class="spanColor" @click="obtaincolor(item)"></span>
          </div>

          </div> -->
          <inputcolors :editcolor="color" @obcolor="changecolors" />

        </el-form-item>
        <!--图标-->
        <el-form-item :label="$t('staff.icon')">
          <el-select
            v-model="form.icon"
            filterable
            :placeholder="$t('operating.pleaseChosen')"
            style="max-width: 215px"
          >
            <el-option
              v-for="item in workTypeList"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            >
              <span
                style="float: left"
              ><i style="font-size: 14px" :class="item.value">{{
                item.label
              }}</i></span>
            </el-option>
          </el-select>
        </el-form-item>
        <!--工种说明-->
        <el-form-item :label="$t('staff.workTypeNote')">
          <el-input v-model="form.note" style="max-width: 215px" />
        </el-form-item>
        <!--工种说明-->
        <el-form-item :label="$t('operating.remark')">
          <el-input v-model="form.remark" style="max-width: 215px" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{
          $t("operating.determine")
        }}</el-button>
        <el-button @click="cancel">{{ $t("operating.cancel") }}</el-button>
      </div>
    </el-dialog>
    <!-- 查看详情 -->
    <el-dialog
      title="查看"
      :visible.sync="details"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!--工种名称-->
        <el-form-item
          :label="$t('staff.staffTypeName')"
          prop="name"
          :rules="filter_rules({ required: '请输入工种名称' })"
        >
          <span>{{ form.name }}</span>
        </el-form-item>
        <!--颜色-->
        <el-form-item :label="$t('staff.color')">
          <el-color-picker v-model="color" />
        </el-form-item>
        <!--图标-->
        <el-form-item :label="$t('staff.icon')">
          <span>{{ form.icon }}</span>
        </el-form-item>
        <!--工种说明-->
        <el-form-item :label="$t('staff.workTypeNote')">
          <span>{{ form.note }}</span>
        </el-form-item>
        <!--工种说明-->
        <el-form-item :label="$t('operating.remark')">
          <span>{{ form.remark }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="details = false">{{
          $t("operating.cancel")
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import inputcolors from '@/components/colors'
import i18n from '@/utils/lang/index'
import {
  resourceTypefindPage,
  resourceTypesave,
  resourceTypedelete
} from '@/api/system/staff'

export default {
  name: 'Post',
  components: { inputcolors
  },
  data() {
    return {

      idx: null,
      color: '',
      workTypeList: [
        { value: 'al-icon-carcustmer', label: '员工' },
        { value: 'al-icon-carshigongrenyuan', label: '施工人员' },
        { value: 'al-icon-carshigongjianli', label: '施工监理' },
        { value: 'al-icon-carlingdao', label: '领导' },
        // {value:'al-icon-carshigongduichang',label:'施工队长'},
        { value: 'al-icon-caryuangong', label: '未定义' },
        { value: 'al-icon-carfangke', label: '访客' },
        { value: 'al-icon-caryuangong1', label: '管理员' }
      ],
      details: false,
      tableHeight: window.innerHeight - 300,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        postName: undefined,
        status: undefined,
        param: {
          type: 'staff'
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: '岗位名称不能为空', trigger: 'blur' }
        ],
        postCode: [
          { required: true, message: '岗位编码不能为空', trigger: 'blur' }
        ],
        postSort: [
          { required: true, message: '岗位顺序不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    changecolors(val) {
      this.color = val
    },

    // 查看详情
    seedetails() {
      this.details = true
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      resourceTypefindPage(this.queryParams).then((response) => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 岗位状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: '0',
        remark: undefined
      }
      this.color = null
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.idx = ''
      this.open = true
      this.title = '新增工种类型'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = {
        name: row.name,
        icon: row.icon,
        note: row.note,
        remark: row.remark
      }
      this.color = row.color

      this.idx = row.idx
      this.open = true
      //  this.editcolor=row.color

      this.title = '修改工种类型'
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title != '修改工种类型') {
            const param = {
              name: this.form.name,
              color: this.color,
              icon: this.form.icon,
              note: this.form.note,
              remark: this.form.remark,
              type: 'staff'
            }
            resourceTypesave(param).then((response) => {
              if (response.code == '200') {
                this.msgSuccess(response.msg)
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            const param = {
              idx: this.idx,
              name: this.form.name,
              color: this.color,
              icon: this.form.icon,
              note: this.form.note,
              remark: this.form.remark,
              type: 'staff'
            }
            resourceTypesave(param).then((response) => {
              if (response.code == '200') {
                this.msgSuccess(response.msg)
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */

    handleDelete(row) {
      const that = this
      const postIds = []
      postIds.push({ idx: row.idx })
      this.$confirm(i18n.t('sxm.delTipItem'), i18n.t('menuManage.title'), {
        confirmButtonText: i18n.t('operating.determine'),

        cancelButtonText: i18n.t('operating.cancel'),
        type: 'warning'
      })
        .then(function() {
          resourceTypedelete(postIds).then((response) => {
            if (response.code == 200) {
              that.getList()
              that.msgSuccess(response.msg)
            } else {
              that.msgError(response.msg)
            }
          })
        })
        .then(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/post/export',
        {
          ...this.queryParams
        },
        `post_${new Date().getTime()}.xls`
      )
    }
  }
}
</script>
<style scoped>
::v-deep .el-color-picker__trigger{
  padding: 0;
    width: 30px;
  height: 30px;
}
.divcolor{
  display: grid;
   grid-template-columns: repeat(4, 1fr);
   width: 180px;
  grid-template-rows: 1fr 1fr;
}
.spanColor {
  display: inline-block;
  width: 30px;
  height: 30px;
}
.typeColor {
  display: inline-block;
  width: 20px;
  height: 20px;
}
</style>
